#!/usr/bin/python
# coding=utf-8


# List of members of parliament
import urllib
import urlparse
import re
#from openpolitics.feedreader.readers import sejmgovpl
from helpers import read_url

patterns = {
    'party_url': re.compile('<LI><A HREF="([^"]*)">Klub</A>', re.I|re.M),
    'shortname': re.compile('(.*)\.htm', re.I|re.M),
}

def join_url(base, url):
    if not url:
        return None
    return urlparse.urljoin(base, url)

def get_shortname(soup):
    match = patterns['shortname'].search(soup)
    return match.group(1)

def get_parties(path=None):
    if not path:
        path = 'http://www.senat.gov.pl/k7/senat/kluby/list-klu.htm'
    source = read_url(path)
    parties = []
    matches = patterns['party_url'].findall(source)
    for match in matches:
        party = {
            'shortname': get_shortname(match),
            'members_url': join_url(path, match),
        }
        parties.append(party)
    return parties

def main():
    parties = get_parties()
    for party in parties:
        print party
    
    
    

if __name__ == "__main__":
    main()
